package com.ghost.librariandb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.text.MaskFormatter;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AddMember extends JPanel
{
	private JTextField txtMemberName;
	private JFormattedTextField txtDOB;
	private JFormattedTextField txtIDNumber;
	private JTextField txtAddress;
	private JFormattedTextField txtPhoneNumber;

	/**
	 * Create the panel.
	 */
	public AddMember()
	{
		setLayout(null);
		
		JLabel lblMemberName = new JLabel("Member Name:");
		lblMemberName.setBounds(10, 11, 100, 14);
		add(lblMemberName);
		
		txtMemberName = new JTextField();
		txtMemberName.setBounds(120, 11, 200, 20);
		add(txtMemberName);
		txtMemberName.setColumns(10);
		
		JLabel lblDob = new JLabel("DOB:");
		lblDob.setBounds(10, 36, 100, 14);
		add(lblDob);
		
		txtDOB = new JFormattedTextField(new java.util.Date());
		txtDOB.setBounds(120, 36, 200, 20);
		add(txtDOB);
		
		JLabel lblIdNumber = new JLabel("ID Number:");
		lblIdNumber.setBounds(10, 61, 100, 14);
		add(lblIdNumber);
		
		MaskFormatter idNumberFormat = null;
		try
		{
			idNumberFormat = new MaskFormatter("#########");
			idNumberFormat.setPlaceholderCharacter('_');
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		txtIDNumber = new JFormattedTextField(idNumberFormat);
		txtIDNumber.setBounds(120, 61, 200, 20);
		add(txtIDNumber);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 86, 100, 14);
		add(lblAddress);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(120, 86, 200, 20);
		add(txtAddress);
		txtAddress.setColumns(10);
		
		JLabel lblPhoneNumber = new JLabel("Phone Number:");
		lblPhoneNumber.setBounds(10, 111, 100, 14);
		add(lblPhoneNumber);
		
		MaskFormatter phoneNumberFormat = null;
		try
		{
			phoneNumberFormat = new MaskFormatter("###-###-###");
			phoneNumberFormat.setPlaceholderCharacter('_');
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		txtPhoneNumber = new JFormattedTextField(phoneNumberFormat);
		txtPhoneNumber.setBounds(120, 111, 200, 20);
		add(txtPhoneNumber);
		
		JButton btnAddMember = new JButton("Add Member");
		btnAddMember.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent event)
			{
				Date now = new Date();
				int memberID = 1;
				Connection connection = DataProvider.getConnection();
				Statement statement = null;
				try
				{
					statement = connection.createStatement();
					statement.setQueryTimeout(30);
					ResultSet rs = statement.executeQuery("select ID from Member order by ID DESC");
					while (rs.next())
					{
						memberID = memberID + rs.getInt("ID");
						break;
					}
					Date dob = (Date) txtDOB.getValue();
					statement.executeUpdate("insert into Member values(" + Integer.toString(memberID) + ", '" + txtMemberName.getText() + "', " + Long.toString(dob.getTime()) + ", '" + txtIDNumber.getText() + "', '" + txtAddress.getText() + "', '" + txtPhoneNumber.getText() + "', " + Long.toString(now.getTime()) + ")");
					txtMemberName.setText("");
					txtDOB.setValue(now);
					txtIDNumber.setValue(null);
					txtAddress.setText("");
					txtPhoneNumber.setValue(null);
				}
				catch (SQLException e)
				{
					e.printStackTrace();
				}
				finally
				{
					if (statement != null)
					{
						try
						{
							statement.close();
						}
						catch (SQLException e)
						{
							e.printStackTrace();
						}
					}
				}
			}
		});
		btnAddMember.setBounds(120, 149, 107, 23);
		add(btnAddMember);

	}
}
